﻿using MCPlatform.Dxf.Utils;
using netDxf;
using netDxf.Entities;
using System;
using System.Drawing;
using System.Net;
using System.Windows.Controls;
using System.Windows.Ink;
using System.Windows.Media;
using static netDxf.Entities.HatchBoundaryPath;

namespace MCPlatform.Dxf.Entities
{
    public class CirclePathEntity : PathEntityBase
    {
       
        public double Radius { get; }
      
       
        public Circle Circle { get; private set; }

        public CirclePathEntity(Circle circle)
        {
            Circle = circle;
            Center = circle.Center; // 保留Z值以支持三维空间中的圆
            Radius = circle.Radius;
            Type = circle;
            Handle = circle.Handle;
            Length = CalculateCircumference(Radius);
            CalculateStartAndEndPoint(circle);
            DrawShape(circle);
        }


        public void DrawShape(Circle? circle, float scaleX = 1, float scaleY = 1, float strokeThickness =1f)
        {
            if (circle is null)
            {
                circle = this.Circle;
            }

            System.Windows.Shapes.Ellipse drawCircle = new System.Windows.Shapes.Ellipse();
            drawCircle.StrokeThickness = Utility.GetNumberFromTextToFloat(circle.Lineweight.ToString(), circle.Layer.Lineweight.ToString()) ?? strokeThickness;
            drawCircle.Stroke = Utility.ConvertAciColorToBrush(circle.Color);
            drawCircle.Width = circle.Radius * 2 * scaleX;
            drawCircle.Height = circle.Radius * 2 * scaleY;
            drawCircle.IsHitTestVisible = false;
            Canvas.SetLeft(drawCircle, (circle.Center.X - circle.Radius) * scaleX);
            Canvas.SetTop(drawCircle, (circle.Center.Y - circle.Radius) * scaleY);

            this.Shape = drawCircle;

            this.Shape.Tag = circle.Handle;
        }


        /// <summary>  
        /// 计算给定半径的圆的周长。  
        /// </summary>  
        /// <param name="radius">圆的半径。</param>  
        /// <returns>返回圆的周长。</returns>  
        public double CalculateCircumference(double radius)
        {
            // 使用圆的周长公式（2πr）来计算周长  
            return 2 * Math.PI * radius;
        }

        /// <summary>  
        /// 计算圆的面积。  
        /// </summary>  
        /// <returns>圆的面积。</returns>  
        public double CalculateArea()
        {
            return Math.PI * Math.Pow(Radius, 2);
        }

        private void CalculateStartAndEndPoint(Circle circle)
        {
            // 计算圆弧的起点和终点
            double startX = circle.Center.X - circle.Radius;
            double startY = circle.Center.Y;
            double endX = circle.Center.X - circle.Radius;
            double endY = circle.Center.Y;

            double centerX = circle.Center.X + circle.Radius;
            double centerY = circle.Center.Y;

            // 如果圆是三维的，则需要考虑 Z 轴坐标
            double startZ = circle.Center.Z;
            double endZ = circle.Center.Z;
            double midZ= circle.Center.Z;
            // 在实际应用中，可能需要根据圆弧的角度进行计算
            // 以下示例假定圆弧是一个完整的圆，即起点和终点为圆周上的两个对称点
            StartPoint = new Vector3(startX, startY, startZ);
            MidPoint = new Vector3(centerX, centerY, midZ);
            EndPoint = new Vector3(endX, endY, endZ);
        }


    }
}
